<?php
abstract class Func_Controller{
	
	protected $smarty;

	public function Func_Controller(){
		
		$this->smarty = new Smarty();

		$this->render();
		
		try{
			$this->renderPage();
		}catch(Exception $e){
			echo "Problem rendering page:" . $e->getMessage() . $e->getTraceAsString();
		}
	}
	
	// override this method in child class
	protected function render(){
		throw new Exception("The method 'render' should be overriden by a sub class");
	}
	
	/**
	 * use to add placement holder values for the view template
	 * 
	 * @param $placeholder
	 * @param $value
	 */
	protected function value($placeholder, $value){
		Validate::notNull($placeholder);
		
		$this->smarty->assign($placeholder, $value);
	}
	
	/**
	 * adds a warning/notification message that can be rendered
	 * at the next request.
	 * 
	 * all previous messages will be detroyed.
	 * 
	 * @param $message
	 */
	protected function flash($message){
		
		//TODO
	}
	
	/*
	 * used to determine the response type.
	 * i.e an ajax request should just render the
	 * ajax response and not the whole page template
	 */
	protected $response;
	
	private function renderPage(){

		if($this->view == null){
			throw new NullPointerException("Please define a view template for this controller.");
		}
		
		/*
		 * echo template if the request
		 * is an ajax request
		 */
		if($this->response == "ajax"){
			echo $this->smarty->fetch($this->view);
			return;
		}
		
		$this->smarty->display($this->view);	
	}
}
?>